package com.qywx.controller;


import com.qywx.entity.SysUserEntity;
import com.qywx.service.SysLoginService;
import com.qywx.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @ClassName SysUserController
 * @Description: 登录相关
 * @Author wmj
 * @Date 2020/3/4
 * @Version V1.0
 **/
@Api(value = "用户登录", tags = {"用户登录"})
@RestController
public class SysLoginController {

    @Autowired
    SysLoginService sysLoginService;

    /**
     * @MethodName: login
     * @Description: 登录
     * @Param: [username, password]
     * @Return: com.qywx.utils.R
     * @Author: wmj
     * @Date: 2020/3/11
     **/
    @RequestMapping(value = "/sys/login", method = RequestMethod.POST)
    @ApiOperation(value = "用户登录", notes = "username:用户名,password:密码")
	/*@ApiImplicitParams({
			@ApiImplicitParam(name = "username", required = true, value = "用户名", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "password", required = true, value = "密码", paramType = "query", dataType = "String")
	})*/
    public R login(@RequestBody SysUserEntity SysUserEntity, HttpServletRequest request) throws NoSuchAlgorithmException {
        return sysLoginService.login(SysUserEntity.getUsername(), SysUserEntity.getPassword(), request);
    }

    @RequestMapping(value = "/sys/detail", method = RequestMethod.POST)
    @ApiOperation(value = "获取登录用户详情", notes = "userid:用户id")
    public R getSysUseEntityDetail(@RequestBody SysUserEntity SysUserEntity) {
        return sysLoginService.getSysUseEntity(SysUserEntity.getUserid());
    }

    @RequestMapping(value = "/sys/save", method = RequestMethod.POST)
    @ApiOperation(value = "新增账号", notes = "必传字段:(username：用户名，password：密码，role：角色，如果是部门管理员，部门id必须传)")
    public R saveSysUser(@RequestBody SysUserEntity SysUserEntity) throws NoSuchAlgorithmException {
        return sysLoginService.saveSysUser(SysUserEntity);
    }

    @RequestMapping(value = "/sys/enable", method = RequestMethod.GET)
    @ApiOperation(value = "启用账号", notes = "")
    @ApiImplicitParams({
			@ApiImplicitParam(name = "userid", required = true, value = "用户id", paramType = "query", dataType = "Integer"),
	})
    public R enableUser(@RequestParam("userid") Integer userid) throws NoSuchAlgorithmException {
        return sysLoginService.enableUser(userid);
    }

    @RequestMapping(value = "/sys/forbidden", method = RequestMethod.GET)
    @ApiOperation(value = "禁用账号", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", required = true, value = "用户id", paramType = "query", dataType = "Integer"),
    })
    public R forbiddenSysUser(@RequestParam("userid") String userid){
        return sysLoginService.forbiddenSysUser(userid);
    }

    @RequestMapping(value = "/sys/update", method = RequestMethod.GET)
    @ApiOperation(value = "修改密码", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", required = true, value = "用户id", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "password", required = true, value = "用户id", paramType = "query", dataType = "String"),
    })
    public R updateSysUser(@RequestParam("userid") String userid,@RequestParam("password") String password) {
        return sysLoginService.updateSysUser(userid,password);
    }

    //所有登录账号
    @GetMapping("/sys/find")
    @ApiOperation(value = "获取所有登录账号", notes = "page：当前页码，size:每页显示条数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", required = true, value = "当前页码", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", required = true, value = "每页显示条数", paramType = "query", dataType = "int")
    })
    public R findUser(@RequestParam("page") int page,@RequestParam("size") int size){
        return sysLoginService.findSysUser(page,size);
    }

    @GetMapping("/sys/logout")
    @ApiOperation(value = "退出", notes = "")
    public R logout(HttpServletRequest request){
        ServletContext servletContext = request.getServletContext();
        List tokens = (List) servletContext.getAttribute("tokens");
        String token = request.getHeader("token");
        if(tokens!=null&&tokens.size()>0){
            tokens.remove(token);
        }
        return R.ok();
    }
}
